﻿package com.snake.core{
	import flash.geom.Point;
	public class FoodInstance extends FoodMC {
		private var index:int=0;
		public function FoodInstance(fx=0, fy=0) {
			try {
				if ((fx==0)&&(fy==0)) {
					index=r_index();
				} else {
					index=Game.map.getTileIndex(new Point(fx,fy));
				}
				var centre:Point=Game.map.getCentreTile(index);
				this.x=centre.x;
				this.y=centre.y;
				Game.main_stage.addChild(this);
				Game.map.setTile(index,"food",true);
			} catch (e) {
				trace("Error in FoodInstance.<init>()");
			}
		}
		private function r_index() {
			return Math.round(Math.random()*Game.map.TILE_AMOUNT);
		}
		public function set_r_pos() {
			index=r_index();
			var centre:Point=Game.map.getCentreTile(index);
			Game.map.setTile(index,"food",true);
			this.x=centre.x;
			this.y=centre.y;
		}
		public function reset() {
			Game.map.setTile(index,"food",false);
		}
		public function destroy() {
			Game.main_stage.removeChild(this);
		}
	}
}